﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using GeoAPI.Geometries;

namespace AzukiMap.Element
{
    /// <summary>
    /// 
    /// </summary>
    public class GeoImage
    {
        public event RoutedEventHandler ImageOpened;
        public event ExceptionRoutedEventHandler ImageFailed;

        BitmapImage _image;
        IPolygon _extent;
        int _width;
        int _height;
        int _srid;

        public BitmapImage Image
        {
            get { return _image; }
        }

        public IPolygon Extent
        {
            get { return _extent; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public int Srid
        {
            get { return _srid; }
            set { _srid = value; }
        }

        public GeoImage(BitmapImage image, IPolygon extent, int srid)
        {
            _image = image;
            _extent = extent;

            // default CreateOptions is DelayCreation that doesn't load image.
            _image.CreateOptions = BitmapCreateOptions.None;
            _image.ImageOpened += new EventHandler<RoutedEventArgs>(_image_ImageOpened);
            _image.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(_image_ImageFailed);
        }

        void _image_ImageOpened(object sender, RoutedEventArgs e)
        {
            _width = ((BitmapImage)e.OriginalSource).PixelWidth;
            _height = ((BitmapImage)e.OriginalSource).PixelHeight;

            if (ImageOpened != null)
            {
                ImageOpened(this, e);
            }
        }

        void _image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            if (ImageFailed != null)
            {
                ImageFailed(this, e);
            }
        }


    }
}
